package com.tianhua.javs.core.macro;

import com.tianhua.javs.client.bean.DomainModelBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.enums.VariablePoolTypeEnum;
import com.tianhua.javs.client.func.IScriptFunc;
import com.tianhua.javs.client.model.FieldModel;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.factory.PredicateFactory;
import com.tianhua.javs.core.factory.VariableFactory;
import com.tianhua.javs.core.parse.JavsContentParseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description:javs宏定义中的全局变量处理解析部分
 * date: 2022/2/16
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
@Slf4j
public class MacroFieldService {

    @Autowired
    private JavsContentParseService javsContentParseService;

    @Autowired
    private VariableFactory variableFactory;

    @Resource(name = "assignFunc")
    private IScriptFunc assignFunc;

    @Resource(name = "springBeanAssignFunc")
    private IScriptFunc springBeanAssignFunc;

    @Autowired
    private PredicateFactory predicateFactory;


    /**
     * 解析宏定义中的属性 作为类的全局变量
     * @param javsFileContentList
     * @param domainModelBean
     * @return
     */
    public List<FieldModel> parseMacroField(List<String> javsFileContentList, DomainModelBean domainModelBean){
        List<String> globaFieldmacroList = javsContentParseService.getGlobalFieldRowLine(javsFileContentList);
        Map<String,List<ScriptVarMappingBean>> javaSDKTypeVarMap = variableFactory.initJavaSDKTypeVarMapping();
        List<FieldModel> fieldModelList = new ArrayList<>();

        for (String javsShellStr : globaFieldmacroList){
            //内置函数 --->特例处理
            //log()
            if(predicateFactory.isSpringBeanField(javsShellStr)){
                TranslateResultBean translateResultBean = springBeanAssignFunc.translate(javsShellStr,javaSDKTypeVarMap);
                if(translateResultBean != null){
                    FieldModel fieldModel = translateResultBean.getMatchSegmentBean().getFieldModel();
                    fieldModel.setClassName(fieldModel.getClassName().trim());
                    String packageName = domainModelBean.searchPackageName(fieldModel.getClassName().trim());
                    if(StringUtils.isNotEmpty(packageName)){
                        fieldModel.getPackageList().add(packageName);
                    }
                    fieldModelList.add(fieldModel);
                }else {
                    log.warn("not match func to replace javsShellStr = "+javsShellStr);
                }
            }
            //函数对象类型声明，走赋值函数提取类型
            else if(javsShellStr.contains(TagConstants.BRACKET_ALL)){

                //获取 = 右边的代码段
                String [] segmentArr = javsShellStr.split("=");
                String rightSegment = segmentArr[1].trim();
                String leftSegment = segmentArr[0].trim();
                String javaCode = replaceModifier(leftSegment, javaSDKTypeVarMap.get(VariablePoolTypeEnum.JAVA_SDK_MODIFIER.getCode()));
                String [] modifierArr = javaCode.split(" ");
                FieldModel fieldModel = new FieldModel();

                if(modifierArr[0].equals("public")){
                    fieldModel.setVisibility("public");
                }
                if(modifierArr[0].equals("private")){
                    fieldModel.setVisibility("private");
                }
                String fieldName = modifierArr[modifierArr.length - 1];
                fieldModel.setFieldSimpleName(fieldName);


                TranslateResultBean translateResultBean = assignFunc.translate(rightSegment,javaSDKTypeVarMap);
                if(translateResultBean != null){
                    String fieldClassType = translateResultBean.getMatchSegmentBean().getSegmentClass();
                    List<String> packageList = translateResultBean.getMatchSegmentBean().getPackageList();
                    fieldModel.addPackageList(packageList);
                    fieldModel.setFieldType(fieldClassType);
                    fieldModel.setFieldName(fieldClassType+" "+fieldName);
                    fieldModel.setInitValue(TagConstants.BLANK_ONE + TagConstants.EQ + TagConstants.BLANK_ONE + translateResultBean.getSegment());
                }
                fieldModelList.add(fieldModel);
            }
        }
        return fieldModelList;
    }


    /**
     * 替换修饰符
     * @param javsShellStr
     * @param javaVarModifierList
     * @return
     */
    private String replaceModifier(String javsShellStr, List<ScriptVarMappingBean> javaVarModifierList){
        String tempShell = javsShellStr;
        for (ScriptVarMappingBean scriptVarMappingBean : javaVarModifierList){
            tempShell = tempShell.replace(scriptVarMappingBean.getJavsVarCode(),scriptVarMappingBean.getJavaCode());
        }
        return tempShell;
    }

    /**
     *
     * @param javsFileContentList
     * @return
     */
    public List<String> parsePackageImport(List<String> javsFileContentList){
        return javsContentParseService.getPackageImportRowLine(javsFileContentList);
    }

}
